package com.xsir.ditan.miniprogram.service.impl;

import com.xsir.ditan.admin.entity.User;
import com.xsir.ditan.admin.mapper.UserMapper;
import com.xsir.ditan.admin.vo.CertVo;
import com.xsir.ditan.miniprogram.entity.Certificate;
import com.xsir.ditan.miniprogram.mapper.CertificateMapper;
import com.xsir.ditan.miniprogram.service.ICertificateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xsir
 * @since 2021-02-08
 */
@Service
public class CertificateServiceImpl extends ServiceImpl<CertificateMapper, Certificate> implements ICertificateService {
    @Autowired
    private CertificateMapper certificateMapper;
    @Autowired
    private UserMapper userMapper;
    
    @Override
    public Map<String, Object> getList() {
        List<CertVo> certVoList = certificateMapper.list();
        Map<String,Object> data = new HashMap<>(2);
        data.put("data",certVoList);
        data.put("code", 0);
        return data;
    }
    
    @Override
    public boolean insertOrUpdate(Certificate certificate) {
        if(certificate == null) {
            return false;
        }
        if(certificate.getId() == null) {
            return save(certificate);
        }
        return certificateMapper.updateWrap(certificate);
    }
    
    @Override
    public Map<String, Object> setStatus(Integer id, Integer status) {
        Certificate certificate = getById(id);
        Integer uid = certificate.getUid();
        User user = userMapper.selectById(uid);
        user.setStatus(status);
        userMapper.updateById(user);
        Map<String,Object> data = new HashMap<>(2);
        data.put("code", 0);
        return data;
    }
}
